package com.webSite.demo.cy.indie.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.webSite.demo.cy.bean.HomeYeWu;
import com.webSite.demo.cy.service.CommonService;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;

@RestController
//@Controller
public class TestController {

	@Autowired
	private CommonService commonService;
	
	@RequestMapping("/order/test")
	@ResponseBody
	public Map<String, Object> getJson() {
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录名
		String name = SecurityContextHolder.getContext().getAuthentication().getName();
		map.put("msg", "success");
		map.put("name", name);
		return map;	
	}
	
	@RequestMapping(value="/downLoad/{name}")
	public void Download(@PathVariable String name,HttpServletResponse response) throws Exception {
		Map<String, Object> row1 = new LinkedHashMap<>();
		row1.put("姓名", "张三");
		row1.put("年龄", 23);
		row1.put("成绩", 88.32);
		row1.put("是否合格", true);
		row1.put("考试日期", DateUtil.date().toString());
		ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1);
		System.out.println("name="+name);
		// 通过工具类创建writer
		ExcelWriter writer = ExcelUtil.getWriter();
		// 定义单元格背景色
		StyleSet style = writer.getStyleSet();
		// 第二个参数表示是否也设置头部单元格背景
		style.setBackgroundColor(IndexedColors.RED, false);
		// 合并单元格后的标题行，使用默认标题样式
		writer.merge(row1.size() - 1, "测试表");
		// 一次性写出内容，使用默认样式
		writer.write(rows);
		
		//response为HttpServletResponse对象
		response.setContentType("application/vnd.ms-excel;charset=utf-8"); 

		try {
			response.setHeader("Content-Disposition", "attachment;fileName="+ new String(name.getBytes("GB2312"),"ISO-8859-1")+".xlsx");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ServletOutputStream out=response.getOutputStream(); 

		writer.flush(out);
		// 关闭writer，释放内存
		writer.close();
	}
	
	@RequestMapping("/cacheDate")
	public HomeYeWu cacheDate(@RequestParam("id")Integer id) {
		HomeYeWu homeYeWu = commonService.getYeWuOne(id);
		System.out.println(homeYeWu);
		return homeYeWu;
	}
	
	@RequestMapping("/cachePut")
	public HomeYeWu cachePut(@RequestParam("id")Integer id) {
		HomeYeWu homeYeWu = commonService.getYeWuOne(id);
		return commonService.updateDate(homeYeWu);
	}
	
	@RequestMapping("/cacheEvict")
	public String cacheEvict(@RequestParam("id")Integer id) {
		return commonService.delYeWuOne(id);
	}
	
	@RequestMapping("/TestAsync")
	public String TestAsync() {
		commonService.TestAsync();
		return "success";
	}

}
